package org.aeon.newsmanagersystem.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aeon.newsmanagersystem.annotation.Protected;
import org.aeon.newsmanagersystem.pojo.Comments;
import org.aeon.newsmanagersystem.pojo.News;
import org.aeon.newsmanagersystem.pojo.Topic;
import org.aeon.newsmanagersystem.pojo.other.PageInfo;
import org.aeon.newsmanagersystem.service.CommentsService;
import org.aeon.newsmanagersystem.service.NewsService;
import org.aeon.newsmanagersystem.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Objects;

/**
 * @author: MissingAeon
 * @date: 2024/3/27 17:28
 * @description:
 */
@Slf4j
@Controller
@RequestMapping("/news")
public class NewsController {

    private final NewsService newsService;
    private final TopicService topicService;
    private final CommentsService commentsService;


    @Autowired
    public NewsController(NewsService newsService, TopicService topicService, CommentsService commentsService) {
        this.newsService = newsService;
        this.topicService = topicService;
        this.commentsService = commentsService;
    }

    @RequestMapping
    public String news(Model model, Integer pageIndex, Integer tid, HttpServletRequest req) {
        final Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                model.addAttribute(cookie.getName(), cookie.getValue());
            }
        }

        final PageInfo<News, Topic> page = new PageInfo<>();
        page.setLimit(25);
        page.setPageIndex(Objects.requireNonNullElse(pageIndex, 1));
        // 设置话题
        if (tid != null) {
            final Topic condition = new Topic();
            condition.setTid(tid);
            page.setCondition(condition);
            model.addAttribute("tid", tid);
        }

        PageInfo<News, Topic> news = newsService.page(page);
        model.addAttribute("news", news);

        List<Topic> topics = topicService.display();
        model.addAttribute("topics", topics);


        return "news";
    }

    @GetMapping("/list")
    public String list(Model model, Integer pageIndex) {
        final PageInfo<News, Topic> page = new PageInfo<>();
        page.setLimit(20);
        page.setPageIndex(Objects.requireNonNullElse(pageIndex, 1));

        PageInfo<News, Topic> news = newsService.page(page);
        model.addAttribute("news", news);

        return "admin/news_list";
    }

    @Protected
    @PostMapping("/add")
    public String add(RedirectAttributes attr, News news, @RequestPart("file") MultipartFile file) {

        final boolean result = newsService.add(news, file);
        if (result) {
            attr.addFlashAttribute("result", "添加成功！");
        } else {
            attr.addFlashAttribute("result", "添加失败！");
        }
        return "redirect:/news/toAdd";
    }

    @Protected
    @GetMapping("/toModify")
    public String toModify(Integer nid, Integer pageIndex, Model model) {
        commentPagination(pageIndex, nid, model);
        return "admin/news_modify";
    }

    @Protected
    @PostMapping("/modify")
    public String modify(RedirectAttributes attr, News news, @RequestPart("file") MultipartFile file) {

        final boolean result = newsService.modify(news, file);
        if (result) {
            attr.addFlashAttribute("result", "修改成功！");
        } else {
            attr.addFlashAttribute("result", "修改失败！");
        }

        return "redirect:/news/list";
    }

    @Protected
    @GetMapping("/delete")
    public String delete(Integer nid, RedirectAttributes attr) {
        final int result = newsService.delete(nid);
        if (result == 1) {
            attr.addFlashAttribute("result", "删除成功！");
        } else {
            attr.addFlashAttribute("result", "删除失败！");
        }
        return "redirect:/news/list";
    }

    @GetMapping("/details")
    public String details(Integer nid, Integer pageIndex, Model model) {
        commentPagination(pageIndex, nid, model);
        return "user/news_read";
    }

    private void commentPagination(Integer pageIndex, Integer nid, Model model) {
        if (pageIndex == null) {
            pageIndex = 1;
        }

        final News found = newsService.get(nid);
        model.addAttribute("news", found);

        PageInfo<Comments, News> pageInfo = new PageInfo<>();
        pageInfo.setLimit(5);
        pageInfo.setCondition(found);
        pageInfo.setPageIndex(pageIndex);
        pageInfo = commentsService.newsComments(pageInfo);
        model.addAttribute("page", pageInfo);
    }
}
